<?php

if (!defined('BASE_PATH'))
	exit('Restricted access');

/**
 *  路由器类
 *  
 *  将外部请求转化为框架内部流程
 *  
 * @author sandy
 * @package sFramework
 *
 */
class Router {

	protected static $instance = null;
	protected $config;
	protected $class = 'demo';
	protected $method = 'index';

	protected function __construct() {
		$this->config = &load_class('Config');
		$this->_set_routing();
	}

	public static function creatFrontControl() {
		if (null === self::$instance) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * 返回当前控制器
	 */
	public function get_class() {
		return $this->class;
	}

	/**
	 * 返回当前方法
	 */
	public function get_method() {
		return $this->method;
	}

	/**
	 * 转发URL到对应的控制器和方法
	 */
	protected function _set_routing() {
		
		// 如果使用查询字符串
		if ($this->config->get_item('url_type') === 'GET') {
			if (isset($_GET[$this->config->get_item('controller_trigger')])) {
				$this->_set_class(trim($_GET[$this->config->get_item('controller_trigger')]));
			} else {
				$this->_set_class(trim($this->config->get_item('default_controller')));
			}

			if (isset($_GET[$this->config->get_item('function_trigger')])) {
				$this->_set_method(trim($_GET[$this->config->get_item('function_trigger')]));
			} else {
				$this->_set_method(trim($this->config->get_item('default_method')));
			}
			return;
		}
		//PATH_INFO
		if ($this->config->get_item('url_type') === 'PATH_INFO') {
			show_error('The "PATH_INFO" rounte method have not finished');
			return;
		}
	}

	
	/**
	 * 设置当前控制器名
	 * 
	 * @param string $className
	 */
	protected function _set_class($className) {
		$this->class = $className;
	}

	/**
	 * 设置当前方法名
	 * 
	 * @param string $methodName
	 */
	protected function _set_method($methodName) {
		$this->method = $methodName;
	}

}